//
//  JWTreeDrawable.m
//  KotH
//
//  Created by James Weatherley on 07/08/2008.
//  Copyright 2008 James Weatherley. All rights reserved.
//

#import "../koth.h"
#import "../wolfiphone.h"
#import "../jwglut/gluUnProject.h"
#import "../geometry/geometry.h"

@implementation JWTreeManager


-(id)initWithEntity:(NSObject*)entity
{
	self = [super init];
	
	assert(entity == nil);
	treeList = [[NSMutableArray alloc] init];
	
	return self;
}

-(void)dealloc
{
	[super dealloc];
	[treeList release];
	pfglDestroyVertexArray(trunks);
	pfglDestroyVertexArray(leaves);
	trunks = NULL;
	leaves = NULL;
}

-(NSObject*)entity
{
	return treeList;
}

-(void)addEntity:(NSObject*)entity
{
	assert([entity isKindOfClass:[JWTree class]]);
	[treeList addObject:entity];
	[self updateVertexArrays];
}

-(void)removeEntity:(NSObject*)entity
{
	assert([entity isKindOfClass:[JWTree class]]);
	JWTree* tree = (JWTree*)entity;
	[tree square].tree = nil;
	[treeList removeObject:tree];
	[self updateVertexArrays];
}

-(void)removeAllEntities
{
	[treeList removeAllObjects];
	[self updateVertexArrays];
}

-(NSArray*)treeList
{
	return treeList;
}

-(void)updateTrees
{
	NSMutableArray* deadWood = [[[NSMutableArray alloc] init] autorelease];
	
	for(JWTree* tree in treeList) {
		if(![tree valid] || tree.square.sentinel || tree.square.player) {
			[deadWood addObject:tree];
		}
	}
	[treeList removeObjectsInArray:deadWood];
}

-(void)updateVertexArrays
{
	pfglDestroyVertexArray(trunks);
	pfglDestroyVertexArray(leaves);
	
	trunks = pfglCreateVertexArray();
	leaves = pfglCreateVertexArray();
	
	GLfloat* colour = 0;
	
	pfglBegin(GL_QUADS);
	for(JWTree* tree in treeList) {
		
		if([tree selected]) {
			colour = selectedColour;
		} else {
			colour = bark;
		}
		
		vec3_t* trunkNormals = [tree trunkNormals];
		vec3_t* trunkVertices = [tree trunkVertices];
		
		for(NSUInteger i = 0; i < 4; ++i) {
			int a = i;
			int b = (i + 1) % 4;
			int c = (i + 1) % 4 + 4;
			int d = i + 4;
			pfglColor4f(colour[0], colour[1], colour[2], 1.0f);
			pfglNormal3f(trunkNormals[i][0], trunkNormals[i][1], trunkNormals[i][2]);
			pfglVertex3f(trunkVertices[a][0], trunkVertices[a][1], trunkVertices[a][2]);
			pfglVertex3f(trunkVertices[b][0], trunkVertices[b][1], trunkVertices[b][2]);
			pfglVertex3f(trunkVertices[c][0], trunkVertices[c][1], trunkVertices[c][2]);
			pfglVertex3f(trunkVertices[d][0], trunkVertices[d][1], trunkVertices[d][2]);
		}
	}
	pfglEndReturnVertexArray(trunks);
	
	pfglBegin(GL_TRIANGLES);
	for(JWTree* tree in treeList) {
		if([tree selected]) {
			colour = selectedColour;
		} else {
			colour = leaf;
		}
		
		vec3_t* treeNormals = [tree treeNormals];
		vec3_t* treeVertices = [tree treeVertices];
		
		NSUInteger indices[4][4] = {{3,0,2,1}, {2,3,0,1}, {1,0,3,2}, {0,1,2,3}};
		for(NSUInteger i = 0; i < 4; ++i) {
			NSUInteger n = indices[i][0];
			NSUInteger v0 = indices[i][1];
			NSUInteger v1 = indices[i][2];
			NSUInteger v2 = indices[i][3];
			
			pfglColor4f(colour[0], colour[1], colour[2], 1.0f);
			pfglNormal3f(treeNormals[n][0], treeNormals[n][1], treeNormals[n][2]);
			pfglVertex3f(treeVertices[v0][0], treeVertices[v0][1], treeVertices[v0][2]);
			pfglVertex3f(treeVertices[v1][0], treeVertices[v1][1], treeVertices[v1][2]);
			pfglVertex3f(treeVertices[v2][0], treeVertices[v2][1], treeVertices[v2][2]);
		}
	}
	pfglEndReturnVertexArray(leaves);
}

-(void)draw
{
	pfglDrawVertexArray(trunks);
	pfglDrawVertexArray(leaves);
}

-(void)postDraw
{
}

@end
